ServerEvents.recipes((event) => {
    let RegisteredFormula = (perenergy, input_items, output_items) => {
        if (input_items.length > 4) return;
        if (output_items.length > 4) return;
        event.recipes.custommachinery.custom_machine("eou:electric_arc_furnace/controller", 200)
            .requireStructure([[
                "ADA",
                "AAC",
                "AmA",
            ], [
                "BBB",
                "B B",
                "BBB",
            ], [
                "BBB",
                "B B",
                "BBB",
            ], [
                "AAA",
                "AEA",
                "AAA",
            ]], {
                "A": "eou:arc_furnace_shell",
                "B": "eou:arc_furnace_coil",
                "C": "eou:electric_arc_furnace/power_port",
                "D": "eou:electric_arc_furnace/exportation_port",
                "E": "eou:electric_arc_furnace/feed_port"
            })
            .requireFunctionToStart((ctx) => {
                let level = ctx.block.minecraftLevel
                let pos = ctx.block.getPos()
                let facing = ctx.getBlock().getProperties().get("facing")
                //控制器其他机器相对坐标
                let ports_pos = { power: [1, 0, -1], exportation: [0, 0, -2], feed: [0, 3, -1] }
                let ports = global.getothermachine(pos, ports_pos, facing, level)
                for (let i in input_items) {
                    let sum = 0, f = 0;
                    for (let j = 0; j < 4; j++) {
                        if (ports["feed"].getItemStored("slot" + j).getId() == input_items[i].name) {
                            sum += ports["feed"].getItemStored("slot" + j).count;
                            if (sum >= input_items[i].count) {
                                f = 1;
                                break;
                            }
                        }
                    }
                    if (f == 0) return Result.error("underinput")
                }
                for (let i in input_items) {
                    let sum = input_items[i].count;
                    for (let j = 0; j < 4; j++) {
                        if (ports["feed"].getItemStored("slot" + j).getId() == input_items[i].name) {
                            let c = ports["feed"].getItemStored("slot" + j).count;
                            if (c > sum) c = sum;
                            ports["feed"].removeItemFromSlot("slot" + j, c, false);
                            sum -= c;
                            if (sum == 0) {
                                break;
                            }
                        }
                    }
                }
                return Result.success()
            })
            .requireFunctionEachTick((ctx) => {
                let level = ctx.block.minecraftLevel
                let pos = ctx.block.getPos()
                let facing = ctx.getBlock().getProperties().get("facing")
                //控制器其他机器相对坐标
                let ports_pos = { power: [1, 0, -1], exportation: [0, 0, -2], feed: [0, 3, -1] }
                let ports = global.getothermachine(pos, ports_pos, facing, level)
                if (ports["power"].getEnergyStored() >= perenergy) {
                    ports["power"].removeEnergy(perenergy, false)
                    return Result.success()
                }
                return Result.error("underpower")
            })
            .requireFunctionOnEnd((ctx) => {
                let level = ctx.block.minecraftLevel
                let pos = ctx.block.getPos()
                let facing = ctx.getBlock().getProperties().get("facing")
                //控制器其他机器相对坐标
                let ports_pos = { power: [1, 0, -1], exportation: [0, 0, -2], feed: [0, 3, -1] }
                let ports = global.getothermachine(pos, ports_pos, facing, level)
                for (let i in output_items) {
                    let sum = output_items[i].count;
                    for (let j = 0; j < 4; j++) {
                        if (ports["exportation"].getItemStored("slot" + j).getId() == output_items[i].name
                            || ports["exportation"].getItemStored("slot" + j).getId() == "minecraft:air") {
                            let c = ports["exportation"].getItemCapacity("slot" + j) - ports["exportation"].getItemStored("slot" + j).count;
                            if (c > sum) c = sum;
                            ports["exportation"].addItemToSlot("slot" + j, Item.of(output_items[i].name, c), false)
                            sum -= c;
                            if (sum == 0) {
                                break;
                            }
                        }
                    }
                }
                return Result.success()
            })
            .resetOnError()
            .jei()
            .produceItem(output_items.length >= 1 ? Item.of(output_items[0].name, output_items[0].count) : Item.empty)
            .produceItem(output_items.length >= 2 ? Item.of(output_items[1].name, output_items[1].count) : Item.empty)
            .produceItem(output_items.length >= 3 ? Item.of(output_items[2].name, output_items[2].count) : Item.empty)
            .produceItem(output_items.length >= 4 ? Item.of(output_items[3].name, output_items[3].count) : Item.empty)
            .requireItem(input_items.length >= 1 ? Item.of(input_items[0].name, input_items[0].count) : Item.empty)
            .requireItem(input_items.length >= 2 ? Item.of(input_items[1].name, input_items[1].count) : Item.empty)
            .requireItem(input_items.length >= 3 ? Item.of(input_items[2].name, input_items[2].count) : Item.empty)
            .requireItem(input_items.length >= 4 ? Item.of(input_items[3].name, input_items[3].count) : Item.empty)
            .requireEnergyPerTick(perenergy)
    }
    RegisteredFormula(100, [//未鉴定的硅晶圆
        { name: "mekanism:dust_quartz", count: 3 },
        { name: "ae2:certus_quartz_dust", count: 3 },
        { name: "minecraft:redstone", count: 3 }
    ], [
        { name: 'eou:unidentified_silicon_wafer', count: 1 }
    ])
})